/**
 *
 * ajax请求方法封装
 * @name successCallback ： 请求成功调用函数
 *
 * @name errorCallback ： 请求失败调用函数
 *
 * */

import { Message } from 'element-ui'

import router from '@/router'
import {setSessionStorage} from './utils'

// 请求成功调用函数
export function successCallback (response) {
  console.log(response)
  console.log(setSessionStorage)
}

// 请求失败调用函数
export function errorCallback (error) {
  let res = error.response
  if (res) {
    if (res.status === 401) {
      Message({
        message: res.data.info,
        center: true,
        type: 'error'
      })
      router.replace('/')
    } else if (res.status === 403) {
      Message({
        message: res.data.info,
        center: true,
        type: 'error'
      })
    } else if (res.status === 503) {
      Message({
        message: res.data.info,
        center: true,
        type: 'error'
      })
    } else {
      Message({
        message: '服务器异常，请稍后重试',
        center: true,
        type: 'error'
      })
    }
    return
  } else if (error.request) {
    // The request was made but no response was received
    // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
    // http.ClientRequest in node.js
    console.log(error.request)
    return
  } else {
    // Something happened in setting up the request that triggered an Error
    console.log('Error', error.message)
  }
  console.log(error.config)
}
